/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	qr_scan.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-05-24
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "DCR55_scan.h"



/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/
static void dcr55_recv_callback(void* arg,uint8_t *buff,uint16_t size);

/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/

__attribute__((section(".RAM_D2"))) static dcr55_t m_dcr55;

static char dcr55_init_buff[]={"++++FWCMSOR1\rCDOPSMD@\rRDFWG\r"};//包含三个命令 使能串口功能 取消持续扫码 版本读取

static char dcr55_version_rtn[]={"<RD><FW MJ=\"1\" MN=\"6\" BU=\"1\" OP="" VS=\"1.6.1\" TY=\"C010603\" DV=\"cd(16.1.44)\" /></RD>\r"};

static char dcr55_start_single[]={"RDCMXEV1,P11,P20\r"};

static char dcr55_stop_single[]={"RDCMXEV1,P10\r"};
//只有发送取消才能取消持续扫码
static char dcr55_start_continuous[]={"CDOPSMD2\r"};

static char dcr55_stop_continuous[]={"CDOPSMD@\r"};

static char dcr55_reset[]={"RDCMXRB1\r"};
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/

dcr55_t* dcr55_init(dcr55_scan_rlt_callback pfn_cb)
{
	m_dcr55.status=DCR55_UNINIT;
	m_dcr55.pfn_scan_rlt_cb =pfn_cb;
	m_dcr55.ack_cnt=0;
	
	m_dcr55.p_uart=bsp_uart_init(QRCODE_DCR55_UART_CHN,DCR55_UART_BAUDRATE);
	
	bsp_uart_tx_dma_init(m_dcr55.p_uart,NULL);
	bsp_uart_rx_dma_init(m_dcr55.p_uart,dcr55_recv_callback,m_dcr55.rx_buff,DCR55_UART_RXBUFF_SIZE);

	bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_init_buff,strlen(dcr55_init_buff));
	m_dcr55.ack_cnt++;
	
	return &m_dcr55;
}
dcr55_status_t dcr55_start(dcr55_status_t new_status)
{
	if(m_dcr55.status == DCR55_UNINIT || m_dcr55.ack_cnt!=0)
	{
		Trace_Log("dcr55 scanner uninit!\r\n");
		bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_init_buff,strlen(dcr55_init_buff));
		m_dcr55.ack_cnt=1;
		return DCR55_ERR;
	}
	switch(new_status){
		case DCR55_SINGLE_SCAN:
			bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_start_single,strlen(dcr55_start_single));
			m_dcr55.status=new_status;
			break;
		case DCR55_CONTINUOUS_SCAN:
			bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_start_continuous,strlen(dcr55_start_continuous));
			m_dcr55.status=new_status;
			break;
		case DCR55_IDLE:
			if(m_dcr55.status==DCR55_SINGLE_SCAN)
			{
				bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_stop_single,strlen(dcr55_stop_single));
			}
			else if(m_dcr55.status==DCR55_CONTINUOUS_SCAN)
			{
				bsp_uart_dma_transfer(m_dcr55.p_uart,dcr55_stop_continuous,strlen(dcr55_stop_continuous));
			}
			m_dcr55.status=new_status;
			break;
		default:break;
	}
	return m_dcr55.status;
}

static void dcr55_recv_callback(void* arg,uint8_t *buff,uint16_t size)
{
	SCB_InvalidateDCache_by_Addr((uint32_t *)buff,size);
	if(m_dcr55.status==DCR55_UNINIT )
	{
		if(memcmp(buff,dcr55_version_rtn,DCR55_VERSION_CHECK_SIZE) == 0)
		if(m_dcr55.ack_cnt) m_dcr55.ack_cnt--;
		m_dcr55.status=DCR55_IDLE;
		return;
	}
	if(m_dcr55.pfn_scan_rlt_cb != NULL)
		m_dcr55.pfn_scan_rlt_cb(&m_dcr55,buff,size);
}
